﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class MainMenu : Menu
    {
        // Current state of MainMenu
        public enum State
        {
            StateLogo,
            StateVideoPlay,
            StateVideoPlaying,
            StateVideoStop,
            StateMenu
        }

        // Event for select main menu
        public delegate void EventHandleMenuSelect(int choice);
        public event EventHandleMenuSelect MenuSelect;

        private static int LOGO_TIME = 5; //By second
        private static int VIDEO_TIME = 1; //By minute
        private MainMenuWithXML _mainMenuWithXMl;
        private GameWindow _window;
        private List<MyButton> _listMenuOption;
        private Sprite2D _backGround;
        private Sprite2D _logo;
        private TimeSpan timer;
        private Video video;
        private Sound _gameMusicc;

        public Sound GameMusic
        {
            get { return _gameMusicc; }
            set { _gameMusicc = value; }
        }
        private VideoPlayer videoPlayer;
        private Texture2D videoTexture;
        private Rectangle videoRect;
        private Sprite2D _pressAnyKey;
        private bool isPressAnyKey = false;

        public Sprite2D PressAnyKey
        {
            get { return _pressAnyKey; }
            set { _pressAnyKey = value; }
        }

        public Sprite2D Logo
        {
            get { return _logo; }
            set { _logo = value; }
        }

        public Sprite2D BackGround
        {
            get { return _backGround; }
            set { _backGround = value; }
        }
        public List<MyButton> ListMenuOption
        {
            get { return _listMenuOption; }
            set { _listMenuOption = value; }
        }

        private float _unitMove = 8.0f;
        public float UnitMove
        {
            get { return _unitMove; }
            set { _unitMove = value; }
        }

        private GameSettingSetup _gameSettingSetup;

        public GameSettingSetup GameSettingSetup
        {
            get { return _gameSettingSetup; }
            set { _gameSettingSetup = value; }
        }

        public MainMenu(GameWindow window, ContentManager content, GraphicsDevice graphicsDevice, string strmenuxml)
        {
            timer = new TimeSpan(0, 0, 0);
            videoPlayer = new VideoPlayer();
            videoRect = new Rectangle(graphicsDevice.Viewport.X, graphicsDevice.Viewport.Y, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);
            GameMusic = new Sound(content, "Sound/audio/Norgard", new List<string>());
            
            _mainMenuWithXMl = content.Load<MainMenuWithXML>(strmenuxml);
            SizeScale = _mainMenuWithXMl.sizeScale;
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            InitMenu(content);
        }

        public void UpdateMenuSize(GraphicsDevice graphicsDevice)
        {
            Size = new Vector2(graphicsDevice.Viewport.Width * SizeScale.X, graphicsDevice.Viewport.Height * SizeScale.Y);
            BackGround.Size = Size;
            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].SetPosition(new Vector2(_mainMenuWithXMl.spriteMenuXML[i].positionScale.X * Size.X, _mainMenuWithXMl.spriteMenuXML[i].positionScale.Y * Size.Y));
                ListMenuOption[i].SetSize(new Vector2(_mainMenuWithXMl.spriteMenuXML[i].sizeScale.X * Size.X, _mainMenuWithXMl.spriteMenuXML[i].sizeScale.Y * Size.Y));
            }
        }
        public void InitMenu(ContentManager content)
        {
            ListMenuOption = new List<MyButton>();
            List<Texture2D[]> listTextureNormal = new List<Texture2D[]>();
            List<Texture2D[]> listTextureHover = new List<Texture2D[]>();
            List<Texture2D[]> listTexturePressed = new List<Texture2D[]>();
            Texture2D[] tempTexture;
            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(_mainMenuWithXMl.strBackGround);
            BackGround = new Sprite2D(tempTexture, new Vector2(0, 0));
            BackGround.Size = Size;

            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(_mainMenuWithXMl.strLogo);
            Logo = new Sprite2D(tempTexture, new Vector2(0, 0));
            video = content.Load<Video>(_mainMenuWithXMl.strVideo);

            tempTexture = new Texture2D[1];
            tempTexture[0] = content.Load<Texture2D>(_mainMenuWithXMl.strPressAnyKey);
            PressAnyKey = new Sprite2D(tempTexture, new Vector2(0, 0));

            ListMenuOption = new List<MyButton>();
            ListMenuOption = LoadListItem(content, _mainMenuWithXMl.spriteMenuXML, "MainMenu/MenuOption/", new Vector2(0, 0));

            for (int i = 0; i < ListMenuOption.Count; i++)
            {
                ListMenuOption[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(ClickButtonWithSender);
            }
        }
        //Process when option was clicked
        public void ClickButtonWithSender(object sender)
        {
            int choice = ListMenuOption.IndexOf((MyButton)sender);
            MenuSelect(choice); 
        }

        public override void Update(GameTime gameTime)
        {
            if (isPressAnyKey == false) timer += gameTime.ElapsedGameTime;
            State state = GetStateFromTimer(timer);
            if (state != State.StateMenu)
            {
                if (Keyboard.GetState().GetPressedKeys().Length > 0)
                {
                    timer = new TimeSpan(0, 1, 0);
                }
                switch (state)
                {
                    case State.StateLogo:
                        Logo.Update(gameTime);             
                        break;
                    case State.StateVideoPlay:
                        videoPlayer.Play(video);
                        videoTexture = videoPlayer.GetTexture();
                        break;
                    case State.StateVideoPlaying:
                        videoTexture = videoPlayer.GetTexture();
                        break;
                    case State.StateVideoStop:
                        videoPlayer.Stop();
                        break;
                    case State.StateMenu:
                        break;
                }
            }
            else
            {
                if (isPressAnyKey == true)
                {
                    BackGround.Update(gameTime);
                    for (int i = 0; i < ListMenuOption.Count; i++)
                    {
                        ListMenuOption[i].Update(gameTime);
                    }
                }
                else
                {
                    BackGround.Update(gameTime);
                    if (timer.Seconds % 2 == 0) PressAnyKey.Update(gameTime);
                }
                if (Keyboard.GetState().GetPressedKeys().Length > 0)
                {
                    isPressAnyKey = true;
                }    
            }
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            State state = GetStateFromTimer(timer);
            switch (state)
            { 
                case State.StateLogo:
                    Logo.Draw(gameTime, spriteBatch);
                    break;
                case State.StateVideoPlay:
                    videoPlayer.Play(video);
                    videoTexture = videoPlayer.GetTexture();
                    spriteBatch.Draw(videoTexture, videoRect, Color.White);
                    break;
                case State.StateVideoPlaying:
                    videoTexture = videoPlayer.GetTexture();
                    spriteBatch.Draw(videoTexture, videoRect, Color.White);
                    break;
                case State.StateVideoStop:
                    videoPlayer.Stop();
                    BackGround.Draw(gameTime, spriteBatch);
                    GameMusic.PlaySong(Volume.MyVolume);
                    GameMusic.RepeatSong = true;
                    if (timer.Seconds % 2 == 0) PressAnyKey.Draw(gameTime, spriteBatch);
                    break;
                case State.StateMenu:
                    if (isPressAnyKey == true)
                    {
                        BackGround.Draw(gameTime,spriteBatch);
                        for (int i = 0; i < ListMenuOption.Count; i++)
                        {
                            ListMenuOption[i].Draw(gameTime, spriteBatch);
                        }
                    }
                    else
                    {
                        BackGround.Draw(gameTime, spriteBatch);
                        if (timer.Seconds % 2 == 0) PressAnyKey.Draw(gameTime, spriteBatch);
                    }
                    if (Keyboard.GetState().GetPressedKeys().Length > 0)
                    {
                        isPressAnyKey = true;
                    }    
                    break;
            }
        }

        public State GetStateFromTimer(TimeSpan gtimer)
        {
            if (gtimer.Minutes == VIDEO_TIME && gtimer.Seconds>0) return State.StateMenu;
            if (gtimer.Minutes == VIDEO_TIME) return State.StateVideoStop;
            if (gtimer.Seconds > LOGO_TIME) return State.StateVideoPlaying;
            if (gtimer.Seconds == LOGO_TIME) return State.StateVideoPlay;
            return State.StateLogo;
        }
    }
}
